layui.use(['jquery','layer','table'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var couponName = "";
    var online = "";
    var grant = "";
    var startDate = "";
    var endDate = "";
    /* 表格渲染开始 */
    var CouponTable = table.render({
        elem:"#table",
        url:"/coupon/getAllCoupon?couponName="+couponName+"&online="+online+"&grant="+grant,
        toolbar:"#tool",
        defaultToolbar: ['','',''],
        page:true,
        limit:7,
        limits:[7,14,21,28],
        cols:[
            [
                {type:"checkbox"},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"startDate",title:"开始时间",align:"center"},
                {field:"endDate",title:"结束时间",align:"center"},
                {field:"online",title:"上线/不上线",align:"center",templet:function (data) {
                        if(data.online=="2"){
                            var radio = '<a href="javascript:updateStatus('+data.couponId+","+data.online+');"><input  type="checkbox" name="sate" lay-skin="switch" lay-text="上线|不上线" lay-filter="switchTest" value="'+data.online+'" checked></a>'
                        }else{
                            var radio = '<a href="javascript:updateStatus('+data.couponId+','+data.online+');"><input  type="checkbox" name="sate" lay-skin="switch" lay-text="上线|不上线" lay-filter="switchTest" value="'+data.online+'"></a>'
                        }
                        return radio;
                    }},
                {field:"grant",title:"发放方式",align:"center",templet:function (data) {
                        if(data.grant=="2"){
                            return "自动发放";
                        }else if(data.grant=="3"){
                            return "活动领取";
                        }else if(data.grant=="4"){
                            return "人工发放";
                        }else if(data.grant=="5"){
                            return "无限制";
                        }
                    }},
                {field:"issueQuantity",title:"发行数",align:"center"},
                {field:"used",title:"已使用数量",align:"center",templet:function (data) {
                        if(data.used != null){
                            return data.used;
                        }else if(data.used == null){
                            return 0;
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        return "<div>"+
                            "<a href='javascript:seeCoupon("+data.couponId+");' style='color: #2F4056;margin-right: 10px'>查看</a>"+
                            "<a href='javascript:delCoupon("+data.couponId+");' style='color: #2F4056'>删除</a><br/>"+
                            "</div>"
                    }}
            ]
        ],
    });
    /* 表格渲染结束 */

    /* 模糊查询开始 */
    form.on('submit(sub)',function (data) {
        var couponName = $("#couponName").val();
        var grant = $("#grant").val();
        var online = $("#online").val();
        CouponTable.reload({
            url:"/coupon/getAllCoupon?couponName="+couponName+"&online="+online+"&grant="+grant
        });
        return false;
    });
    /* 模糊查询结束 */

    /* 重置开始 */
    form.on('submit(subs)',function (data) {
        CouponTable.reload({
            url:"/coupon/getAllCoupon?couponName="+couponName+"&online="+online+"&grant="+grant
        });
    });
    /* 重置结束 */

    /* 修改上线状态开始 */
    updateStatus = function updateStatus(couponId,online){
        if(online == 2){
            var online2 = 3;
        }else{
            var online2 = 2;
        };

        var updateId = localStorage.getItem("userId");

        $.ajax({
            url:"/coupon/updateCoupon",
            type:"post",
            dataType:"json",
            data:{
                "couponId":couponId,
                "online":online2,
                "updateId":updateId
            },
            success:function(data){
                if(data){
                    layer.msg("修改成功");
                }else{
                    layer.msg("修改失败");
                }
                location.reload();
            }
        });
    };
    /* 修改上线状态结束 */

    /* 查看详情开始 */
    seeCoupon = function seeCoupon(couponId) {
        $.ajax({
            url:"/coupon/seeCoupon",
            data:{"couponId":couponId},
            dataType:"json",
            Type:"post",
            success:function (data) {
                selectCoupon(data);
            }
        });
    };
    function selectCoupon(data){
        layer.open({
            type:2,
            maxmin:true,
            title:"查看优惠券详情",
            content:"/coupon/coupon-see.html",
            area:['1200px', '550px'],
            btn:["确定","取消"],
            yes:function (index) {
                layer.close(index);
            },
            btn2:function (index) {
                layer.close(index);
            },
            success:function (layero,index) {
                var obj = layero.find('iframe').contents();
                obj.find("#couponCode").val(data.data.couponCode);
                obj.find("#couponName").val(data.data.couponName);
                obj.find("#couponPrice").val(data.data.couponPrice);
                obj.find("#issueQuantity").val(data.data.issueQuantity);
                obj.find("#used").val(data.data.used);
                obj.find("#startDate").val(data.data.startDate);
                obj.find("#pricuteUrl").attr('src',"http://"+data.data.pricuteUrl);
                obj.find("#trueName").val(data.data.trueName);
                obj.find("#createDate").val(data.data.createDate);
                obj.find("#endDate").val(data.data.endDate);
                //状态
                var online = data.data.online;
                if(online=="2"){
                    online="上线";
                }else{
                    online="不上线";
                }
                obj.find("#online").val(online);
                //发放方式
                var grant = data.data.grant;
                if(grant=="2"){
                    grant="上线";
                }else if(grant=="3"){
                    grant="活动领取";
                }else if(grant=="4"){
                    grant="人工发放";
                }else{
                    grant="无限制";
                }
                obj.find("#grant").val(grant);
                //类型
                var type = data.data.type;
                if(type=="2"){
                    type="商品";
                }else{
                    type="视频";
                }
                obj.find("#type").val(type);
            }
        });
    }
    /* 查看详情结束 */

    /* 删除开始 */
    delCoupon = function delCoupon(couponId) {
        layer.confirm('是否确定拦截此优惠卷', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url:"/coupon/delCoupon",
                    data:{"couponId":couponId},
                    dataType:"json",
                    Type:"post",
                    success:function(data){
                        location.reload();
                    }
                });
            }
        );
    }
    /* 删除结束 */

    /* 高级检索开始 */
    $("#retrieval").click(function () {
        layer.open({
            type:2,
            maxmin:true,
            title:"高级检索",
            shade: 0,
            area: ['600px', '500px'],
            content:"/coupon/coupon-retrieval.html",
            btn:["确定","取消"],
            yes:function (index) {
                var couponName = obj.find("#couponName").val();
                var grant = obj.find("#grant").val();
                var online = obj.find("#online").val();
                var startDate = obj.find("#startDate").val();
                var endDate = obj.find("#endDate").val();
                CouponTable.reload({
                    url:"/coupon/getAllCoupon?couponName="+couponName+"&online="+online+"&grant="+grant+"&startDate="+startDate+"&endDate="+endDate
                });
                layer.close(index);
            },
            btn2:function (index) {
                layer.close(index);
            },
            success:function (layero,index) {
                obj = layero.find('iframe').contents();
            }
        })
    })
    /* 高级检索结束 */

    /* 返回开始 */
    goBack = function goBack() {
        //通过页面查找iframe
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
    /* 返回结束 */

    /* 刷新开始 */
    Refresh = function Refresh() {
        location.reload();
    };
    /* 刷新结束 */
});